#include <google/protobuf/stubs/callback.h>
#include "calculator.pb.h"
#include "fastrpc/rpc.hpp"
using namespace fastrpc::log;
using namespace fastrpc::io;
using namespace fastrpc::net;
using namespace fastrpc::async;
using namespace fastrpc;
using namespace fastrpc::rpc;

class CalculatorImpl : public Calculator {
    void Add(google::protobuf::RpcController* controller,
             const ::CalculatorRequest*       request,
             ::CalculatorResponse*            response,
             ::google::protobuf::Closure*     done) {
        response->set_result(request->m() + request->n());
        if (done) {
            done->Run();
        }
    }
    void Subtract(google::protobuf::RpcController* controller,
                  const ::CalculatorRequest*       request,
                  ::CalculatorResponse*            response,
                  ::google::protobuf::Closure*     done) {
        response->set_result(request->m() - request->n());
        if (done) {
            done->Run();
        }
    }
    void Multiply(google::protobuf::RpcController* controller,
                  const ::CalculatorRequest*       request,
                  ::CalculatorResponse*            response,
                  ::google::protobuf::Closure*     done) {
        response->set_result(request->m() * request->n());
        if (done) {
            done->Run();
        }
    }
    void Divide(google::protobuf::RpcController* controller,
                const ::CalculatorRequest*       request,
                ::CalculatorResponse*            response,
                ::google::protobuf::Closure*     done) {
        response->set_result(request->m() / request->n());
        if (done) {
            done->Run();
        }
    }
};

int main() {
    SET_LOG_LEVEL(fastrpc::log::LogLevel::Debug);
    RpcServer      server{10000};
    FastRpcClosure cb([]() { std::cout << "finished\n"; });
    server.set_callback(&cb);
    std::shared_ptr<CalculatorImpl> service = std::make_shared<CalculatorImpl>();
    server.register_service(service);
    server.run();
    return 0;
}